﻿package com.tomvian.game.control
{
	import flash.ui.Keyboard;
	
	/**
	 * Controller
	 * @author Thomas Vian
	 */
	dynamic public class Controller 
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		internal var keyList:Array;
		internal var namesList:Array;
		internal var namesNum:uint;
		
		internal var keyEnabled:Boolean;
		
		public var keyPress:Object;
		
		//--------------------------------------------------------------------------
		//	
		// Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		public function get enabled():Boolean { return keyEnabled; }
		public function set enabled(value:Boolean):void
		{
			if (!value && keyEnabled)
			{
				for (var i:uint = 0; i < namesNum; i++)
				{
					this[namesList[i]] = false;
				}
			}
			
			keyEnabled = value;
		}
		
		//--------------------------------------------------------------------------
		//	
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function Controller()
		{
			keyList = [];
			namesList = [];
			keyEnabled = true;
			
			keyPress = { };
		}
		
		//--------------------------------------------------------------------------
		//	
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		public function addKey(name:String, key:String):void
		{
			var code:uint = Keyboard[key.toUpperCase()];
			
			if (code == 0)
			{
				switch(key.toLowerCase())
				{
					case "a": {code = 65; break;} case "b": {code = 66; break;} case "c": {code = 67; break;}
					case "d": {code = 68; break;} case "e": {code = 69; break;} case "f": {code = 70; break;}
					case "g": {code = 71; break;} case "h": {code = 72; break;} case "i": {code = 73; break;}
					case "j": {code = 74; break;} case "k": {code = 75; break;} case "l": {code = 76; break;}
					case "m": {code = 77; break;} case "n": {code = 78; break;} case "o": {code = 79; break;}
					case "p": {code = 80; break;} case "q": {code = 81; break;} case "r": {code = 82; break;}
					case "s": {code = 83; break;} case "t": {code = 84; break;} case "u": {code = 85; break;}
					case "v": {code = 86; break;} case "w": {code = 87; break;} case "x": {code = 88; break;}
					case "y": {code = 89; break;} case "z": {code = 90; break;} case "0": {code = 48; break;}
					case "1": {code = 49; break;} case "2": {code = 50; break;} case "3": {code = 51; break;}
					case "4": {code = 52; break;} case "5": {code = 53; break;} case "6": {code = 54; break;}
					case "7": {code = 55; break;} case "8": {code = 56; break;} case "9": {code = 57; break;}
				}
			}
			
			if (code > 0) 
			{
				if (keyList[code]) 	keyList[code].push(name);
				else  				keyList[code] = [name];
				namesList.push(name);
				namesNum++;
				this[name] = false;
			}
		}
		
		public function addKeyCode(name:String, key:uint):void
		{
			if (keyList[key]) 	keyList[key].push(name);
			else  				keyList[key] = [name];
			
			namesList.push(name);
			namesNum++;
			this[name] = false;
		}
		
		public function removeKeyCode(name:String, key:uint):void
		{
			if (!keyList[key]) return;
			
			if (keyList[key].length == 1) 
			{
				keyList[key] = null;
			}
			else 
			{
				for (var i:int = 0, l:int = keyList[key].length; i < l; i++)
				{
					if (keyList[key][i] == name)
					{
						keyList.splice(i, 1);
						break;
					}
				}
			}
			
			for (i = 0, l = namesList.length; i < l; i++)
			{
				if (namesList[i] == name)
				{
					namesList.splice(i, 1);
					i--;
					l--;
				}
			}
			
			this[name] = false;
			keyPress[name] = false;
		}
	}
}